home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 May / com_0505_1.iso / opensource / top10 / amc_install.exe / {app} / Scripts / All Movie Guide (pic).ifs < prev    next >
Encoding:
Text File  |  2004-10-21  |  9.0 KB  |  250 lines

  1. // GETINFO SCRIPTING
  2. // All Movie Guide (US) import
  3.  
  4. (***************************************************
  5.  *  Movie importation script for:                  *
  6.  *  All Movie Guide (US), http://allmovie.com      *
  7.  *                                                 *
  8.  *  (c) 2003 Hubert Kosior                         *
  9.  *       send bugs and reports to: hubert@tm1.net  *
  10.  *   a bug corrected by Antoine Potten             *
  11.  *                                                 *
  12.  *  For use with Ant Movie Catalog 3.4.1           *
  13.  *  www.antp.be/software/moviecatalog              *
  14.  *                                                 *
  15.  *  This program is free software; you can         *
  16.  *  redistribute it and/or modify it under the     *
  17.  *  terms of the GNU General Public License as     *
  18.  *  published by the Free Software Foundation;     *
  19.  *  either version 2 of the License, or (at your   *
  20.  *  option) any later version.                     *
  21.  ***************************************************)
  22.  
  23. // to do:
  24. // - producer's name instad of producing company
  25. // - display movie categories when movie list hit (after searching)
  26.  
  27. program AllMovie;
  28. var
  29.   MovieName: string;
  30.  
  31. // simple string procedures
  32. function StringReplaceAll(S, Old, New: string): string;
  33. begin
  34.   while Pos(Old, S) > 0 do
  35.     S := StringReplace(S, Old, New);
  36.   Result := S;
  37. end;
  38. procedure CutAfter(var Str: string; Pattern: string);
  39. begin
  40.   Str := Copy(str, Pos(Pattern, Str) + Length(Pattern), Length(Str));
  41. end;
  42. procedure CutBefore(var Str: string; Pattern: string);
  43. begin
  44.   Str := Copy(Str, Pos(Pattern, Str), Length(Str));
  45. end;
  46.  
  47. // Loads and analyses page from internet (list of movies or direct hit)
  48. procedure AnalyzePage(Address: string);
  49. var
  50.   Page: TStringList;
  51. begin
  52.   Page := TStringList.Create;
  53.   Page.Text := GetPage(Address);
  54.   // movie list
  55.   if Pos('movie titles like: ', Page.Text) > 0 then
  56.   begin
  57.     PickTreeClear;
  58.     PickTreeAdd('Search results', '');
  59.     AddMoviesTitles(Page);
  60.     if PickTreeExec(Address) then
  61.       AnalyzePage(Address);
  62.   // refine search
  63.   end
  64.   else
  65.   if Pos('Sorry, there is too many possible matches, please adjust your search.', Page.Text) > 0 then
  66.   begin
  67.     ShowMessage('Sorry, there is too many possible matches, please adjust your search.');
  68.     if Input('All Movie Import', 'Enter the title of the movie:', MovieName) then
  69.       AnalyzePage('http://allmovie.com/cg/avg.dll?p=avg&type=2&srch=' + URLEncode(MovieName));
  70.   // direct hit
  71.   end
  72.   else
  73.   begin
  74.     SetField(FieldURL, Address);
  75.     AnalyzeMoviePage(Page)
  76.   end;
  77. end;
  78.  
  79. // Extracts movie details from page
  80. procedure AnalyzeMoviePage(MoviePage: TStringList);
  81. var
  82.   Page: string;
  83.   Value: string;
  84. begin
  85.   Page := MoviePage.Text;
  86.  
  87.   // Original title
  88.   SetField(fieldOriginalTitle, GetStringFromHTML(Page, '<TITLE>', ': ', '</TITLE>'));
  89.  
  90.   // Year
  91.   SetField(fieldYear, GetStringFromHTML(Page, '<B>'+GetField(fieldOriginalTitle)+'</B>', '</TR>', '</B>'));
  92.  
  93.   // Country
  94.   SetField(fieldCountry, GetStringFromHTML(Page, '<B>'+GetField(fieldOriginalTitle)+'</B>', '<I>', '</I>'));
  95.  
  96.   // Length
  97.   SetField(fieldLength, GetStringFromHTML(Page, '<B>'+GetField(fieldOriginalTitle)+'</B>', '</I> - ', ' min'));
  98.  
  99.   // AKA -> translated title
  100.   SetField(fieldTranslatedTitle, GetStringFromHTML(Page, '>AKA', '</TD>', '</td>'));
  101.  
  102.   // Rating (multiplied by 2, because 0 <= AMG rating <= 5)
  103.   Value := GetStringFromHTML(Page, '>AMG Rating', 'alt="', ' Stars');
  104.   if Length(Value) > 0 then SetField(fieldRating, FloatToStr(StrToFloat(Value)*2));
  105.  
  106.   // Director
  107.   SetField(fieldDirector, GetStringFromHTML(Page, '>Director', '</TD>', '</td>'));
  108.  
  109.   // Genre -> category
  110.   SetField(fieldCategory, GetStringFromHTML(Page, '>Genre/Type', '</TD>', '</td>'));
  111.  
  112.   // Producing company  -> producer
  113.   SetField(fieldProducer, GetStringFromHTML(Page, '>Produced by', '</TD>', '</TD>'));
  114.  
  115.   // Image
  116.   Value := GetStringFromHTML(Page, 'http://image.allmusic.com', '', '"');
  117.   if Length(Value) > 0 then GetPicture(Value, False);
  118.  
  119.   // Plot synopsis -> description
  120.   Value := GetStringFromHTML(Page, '<A Name="PLOT">', '</table>', '</table>');
  121.   if Length(Value) > 0 then SetField(fieldDescription, 'PLOT SYNOPSIS:'+#13#10+Value+#13#10);
  122.  
  123.   // Review -> description
  124.   Value := GetStringFromHTML(Page, '<A Name="REVIEW">', '</table>', '</table>');
  125.   if Length(Value) > 0 then SetField(fieldDescription, GetField(fieldDescription)+'AMG REVIEW:'+#13#10+Value+#13#10);
  126.  
  127.   // Awards -> description
  128.   // adjust spaces and line feeds
  129.   Value := StringReplaceAll(Page, '> <FONT', ''); // space before title
  130.   Value := StringReplaceAll(Value, '</FONT> </td><td WIDTH=209>', ' - '); // minus before name
  131.   Value := StringReplaceAll(Value, ' </A></FONT></td>', ' - '); // minus after name (1)
  132.   Value := StringReplaceAll(Value, ' </FONT></td>', ' - '); // minus after name (2)
  133.   Value := StringReplaceAll(Value, '</FONT> </td></tr>', + #13#10); // newline after academy name
  134.   Value := GetStringFromHTML(Value, '<A Name="AWRD">', '</td></tr>', '</TABLE>');
  135.   Value := StringReplaceAll(Value, '  ', ' ');
  136.   Value := StringReplaceAll(Value, ' - - ', ' - ');
  137.   if Length(Value) > 0 then SetField(fieldDescription, GetField(fieldDescription)+'AWARDS:'+#13#10+Value);
  138.  
  139.   // remove trailing newline from description
  140.   Value := GetField(fieldDescription);
  141.   if Copy(Value, Length(Value) - 1, 2) = #13#10 then begin
  142.     Value := Copy(Value, 0, Length(Value) - 2);
  143.     SetField(fieldDescription, Value);
  144.   end;
  145.  
  146.   // Cast -> actors
  147.   // adjust semicolons
  148.   Value := StringReplaceAll(Page, '</I></TD></TR>', '; ');
  149.   Value := GetStringFromHTML(Value, '<A Name="CAST">', '</td></tr>', '</TABLE>');
  150.   if Length(Value) > 0 then begin
  151.     // remove double spaces if only actor name given
  152.     while Pos('  ', Value) > 0 do
  153.       Delete(Value, Pos('  ', Value), 2);
  154.     // remove trailing "; "
  155.     if Copy(Value, Length(Value) - 1, 2) = '; ' then
  156.       Value := Copy(Value, 0, Length(Value) - 2);
  157.     SetField(fieldActors, Value)
  158.   end;
  159.  
  160.   DisplayResults;
  161. end;
  162.  
  163. // Adds movie titles from search results to tree
  164. procedure AddMoviesTitles(ResultsPage: TStringList);
  165. var
  166.   Page: string;
  167.   MovieTitle, MovieAddress: string;
  168. begin
  169.   Page := ResultsPage.Text;
  170.   // Every movie entry begins with string "<A HREF='/cg/avg.dll?"
  171.   while Pos('<A HREF="/cg/avg.dll?', Page) > 0 do
  172.   begin
  173.     CutBefore(Page, '<A HREF="/cg/avg.dll?');
  174.     MovieAddress := 'http://allmovie.com' + GetStringFromHTML(Page, '<A', '"', '">');
  175.     MovieTitle := GetStringFromHTML(Page, '<A', '', '</tr>');
  176.     MovieTitle := StringReplace(MovieTitle, ')', '),  ');
  177.     CutAfter(Page, '</tr>');
  178.     // add movie to list
  179.     PickTreeAdd(MovieTitle, MovieAddress);
  180.   end;
  181. end;
  182.  
  183. // Extracts single movie detail (like director, genre) from page
  184. function GetStringFromHTML(Page, StartTag, CutTag, EndTag: string): string;
  185. begin
  186.   Result := '';
  187.   // recognition tag - if present, extract detail from page, otherwise assume detail is not present
  188.   if Pos(StartTag, Page) > 0 then begin
  189.     CutBefore(Page, StartTag);
  190.     // optional cut tag helps finding right string in html page
  191.     if Length(CutTag) > 0 then
  192.       CutAfter(Page, CutTag);
  193.     // movie detail copied with html tags up to end string
  194.     Result := Copy(Page, 0, Pos(EndTag, Page) - 1);
  195.     // remove html tags and decode html string
  196.     HTMLRemoveTags(Result);
  197.     HTMLDecode(Result);
  198. //  ShowMessage('DEBUG: GetStringFromHTML - StartTag "'+StartTag+'", CutTag "'+CutTag+'", EndTag "'+EndTag+'", Result "'+Result+'" ___ '+Page);
  199.   end;
  200. end;
  201.  
  202. procedure RemovePronoun(var Str: string);
  203. var
  204.   i: Integer;
  205.   s: string;
  206.   c: char;
  207. begin
  208.   // remove pronouns
  209.   s := UpperCase(Copy(Str, 0, 4));
  210.   if (s = 'LES ') or (s = 'UNE ') or (s = 'THE ') then
  211.     Str := Copy(Str, 5, Length(Str) - 4)
  212.   else
  213.   begin
  214.     s := Copy(s, 0, 3);
  215.     if (s = 'LE ') or (s = 'UN ') then
  216.       Str := Copy(Str, 4, Length(Str) - 3)
  217.     else
  218.     begin
  219.       s := Copy(s, 0, 2);
  220.       if (s = 'L''') or (s = 'L ') or (s = 'A ') then
  221.         Str := Copy(Str, 3, Length(Str) - 2)
  222.     end;
  223.   end;
  224.   // remove non-letters, non-digits and non-spaces
  225.   s := '';
  226.   for i := 1 to Length(Str) do begin
  227.   c := StrGet(Str, i);
  228.     if ((c<'a') or (c>'z')) and
  229.        ((c<'A') or (c>'Z')) and
  230.        ((c<'0') or (c>'9')) and
  231.        (c<>' ') then
  232.     else
  233.       s := s + Copy(Str, i, 1);
  234.   end;
  235.   Str := s;
  236. end;
  237.  
  238. begin
  239.   if CheckVersion(3,4,1) then begin
  240.     MovieName := GetField(fieldOriginalTitle);
  241.     if MovieName = '' then MovieName := GetField(fieldTranslatedTitle);
  242.     if Input('All Movie Import', 'Enter the title of the movie (only letters, digits and spaces):', MovieName) then
  243.     begin
  244.       RemovePronoun(MovieName);
  245.       AnalyzePage('http://allmovie.com/cg/avg.dll?p=avg&type=2&srch=' + StringReplace(URLEncode(MovieName), '%20', '+'));
  246.     end;
  247.   end
  248.     else ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.1)');
  249. end.
  250.